﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WebLibs;

namespace MDF.DataHelper.DataObject.UsersDO
{
    public class UserProfileDAO : BaseDataObject
    {
        #region Constructor

        public UserProfileDAO()
        {
        }

        public UserProfileDAO(IData _objData)
        {
            this.m_objData = _objData;
        }
        #endregion

        #region Methods

        public DataTable GetAll()
        {
            IData objData = this.m_objData == null ? new IData() : this.m_objData;
            DataTable dtResult = new DataTable();
            try
            {
                objData.Connect();
                objData.CreateNewStoredProcedure("[UserRoles_Select]");
                dtResult = objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (this.m_objData == null) objData.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetByUsernamePassword(string username, string password)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_Login]");
                this.m_objData.AddParameter("@Username", username);
                this.m_objData.AddParameter("@Password", password);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetByUserID(int userId)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetByUserID]");
                this.m_objData.AddParameter("@UserID", userId);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetByUsername(string username)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetByUsername]");
                this.m_objData.AddParameter("@Username", username);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetChilds(int UserID)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetChilds]");
                this.m_objData.AddParameter("@UserID", UserID);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetList(string Keyword, string lstUserID, int Page, int PageSize)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetList]");
                this.m_objData.AddParameter("@ListUserID", lstUserID);
                this.m_objData.AddParameter("@Keyword", Keyword);
                this.m_objData.AddParameter("@Page", Page);
                this.m_objData.AddParameter("@PageSize", PageSize);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public DataTable GetByGroup(int _iGroupID)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetByGroup]");
                this.m_objData.AddParameter("@GroupID", _iGroupID);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public int Insert(int UserId,
                        int ManagerID,
                        string UserName,
                        string Name,
                        string Email,
                        string Phone,
                        DateTime Birthday,
                        string Sex,
                        string Address,
                        string Avatar,
                        string Domain,
                        bool IsLocked)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_Insert]");
                this.m_objData.AddParameter("@UserId", UserId);
                if(ManagerID>0)
                    this.m_objData.AddParameter("@ManagerID", ManagerID);
                this.m_objData.AddParameter("@UserName", UserName);
                this.m_objData.AddParameter("@Name", Name);
                this.m_objData.AddParameter("@Email", Email);
                this.m_objData.AddParameter("@Phone", Phone);
                if (Birthday.Year > 1)
                    this.m_objData.AddParameter("@Birthday", Birthday);
                this.m_objData.AddParameter("@Sex", Sex);
                this.m_objData.AddParameter("@Address", Address);
                this.m_objData.AddParameter("@Avatar", Avatar);
                this.m_objData.AddParameter("@Domain", Domain);
                this.m_objData.AddParameter("@IsLocked", IsLocked);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            if (dtResult == null || dtResult.Rows.Count == 0)
                return -1;
            return Int32.Parse(dtResult.Rows[0][0].ToString());
        }

        public DataTable GetAll(string _strSearch, int _iUserGroupID, int _iManageID, int _iPage, int _iPageSize)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_GetAll]");
                this.m_objData.AddParameter("@Search", _strSearch);
                this.m_objData.AddParameter("@ManageID", _iManageID);
                this.m_objData.AddParameter("@UserGroupID", _iUserGroupID);
                this.m_objData.AddParameter("@Page", _iPage);
                this.m_objData.AddParameter("@PageSize", _iPageSize);
                dtResult = this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public void Delete(int iUserID, int DeletedUser)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_Delete]");
                this.m_objData.AddParameter("@UserID", iUserID);
                this.m_objData.AddParameter("@DeletedUser", DeletedUser);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        #endregion

        public void Update(int UserId, int ManagerID,
                            string UserName, string Name,
                            string Email, string Phone,
                            DateTime Birthday, string Sex,
                            string Address, string Avatar,
                            string Domain, bool IsLocked)
        {

            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserProfile_Update]");
                this.m_objData.AddParameter("@UserId", UserId);
                //this.m_objData.AddParameter("@ManagerID", ManagerID);
                this.m_objData.AddParameter("@UserName", UserName);
                this.m_objData.AddParameter("@Name", Name);
                this.m_objData.AddParameter("@Email", Email);
                this.m_objData.AddParameter("@Phone", Phone);
                this.m_objData.AddParameter("@Birthday", Birthday);
                this.m_objData.AddParameter("@Sex", Sex);
                this.m_objData.AddParameter("@Address", Address);
                this.m_objData.AddParameter("@Avatar", Avatar);
                this.m_objData.AddParameter("@Domain", Domain);
                this.m_objData.AddParameter("@IsLocked", IsLocked);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
    }
}
